'use strict'; //评论规范
const Controller = require('egg').Controller;
class comment_specificationController extends Controller {
    async index() { //模糊搜索跟分页查询
        const {
            ctx,
            app
        } = this;
        const {
            keyname,
            page = 1,
            number = 10
        } = ctx.request.query
        const offset = (page - 1) * number
        let data, count
        if (keyname && page) {
            data = await app.mysql.query(`select id,title,start,content,set_time from comment_specification where id like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from comment_specification where id like '%${keyname}%'`)
        } else if (keyname) {
            data = await app.mysql.query(`select id,title,start,content,set_time from comment_specification where id ='%${keyname}%' or id  like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from id where id like '%${keyname}%' or id like '%${keyname}%'`)
        } else {
            data = await app.mysql.query(`select id,title,start,content,set_time from comment_specification limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from comment_specification`)
        }
        ctx.body = {
            code: 0,
            msg: "查找成功",
            data,
            count: count
        }
    }

    async update() {
        const {
            ctx,
            app
        } = this;
        const {
            title,
            start,
            content,
            set_time
        } = ctx.request.body;
        const {
            id
        } = ctx.params;
        const row = {
            title,
            start,
            content,
            set_time
        }
        const options = {
            where: {
                id: +id
            }
        };
        const result = await app.mysql.update('comment_specification', row, options)
        if (title, start, content, set_time) {
            ctx.body = {
                code: 0,
                msg: "修改成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 408,
                msg: "修改失败",
            };
        }
    }

}
module.exports = comment_specificationController;